package com.lyfei.ydc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyfei.ydc.domain.entity.Banner;
import com.lyfei.ydc.domain.entity.BannerItem;
import com.lyfei.ydc.domain.vo.BannerVo;
import com.lyfei.ydc.exception.http.NotFoundException;
import com.lyfei.ydc.mapper.BannerMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("bannerService")
public class BannerService extends BaseService<BannerMapper, Banner> {
    @Autowired
    private BannerItemService bannerItemService;

    private BannerVo toBannerVo(Banner banner) {
        BannerVo bannerVo = new BannerVo();
        if(banner == null) {
            throw new NotFoundException(30005);
        }

        BeanUtils.copyProperties(banner, bannerVo);

        bannerVo.setBannerItems(bannerItemService.list(new LambdaQueryWrapper<BannerItem>()
                .eq(BannerItem::getBannerId, banner.getId())
                .eq(BannerItem::getDeleted, false)));

        return bannerVo;
    }

    public BannerVo getBannerDetailByName(String name) {
        Banner banner = this.getByName(name);
        if(banner == null) {
            throw new NotFoundException(30005);
        }

        return this.toBannerVo(banner);
    }

    public Banner getByName(String name) {
        return this.getOne(new LambdaQueryWrapper<Banner>()
                .eq(Banner::getName, name)
                .eq(Banner::getDeleted, false));
    }
}
